import { loadCSS, loadJS } from '../../utils/loadJS'

const baseUrl = import.meta.env.VITE_FLOWABLE_URL

export const loadPreviewDepsAction = () => {
  return async (dispatch) => {
    try {
      await loadCSS(baseUrl + 'modeler/display/jquery.qtip.min.css')
      await loadCSS(baseUrl + 'modeler/display/displaymodel.css')
      await loadJS(baseUrl + 'modeler/display/jquery.qtip.min.js')
      await loadJS(baseUrl + 'modeler/display/raphael.min.js')
      await loadJS(baseUrl + 'modeler/display/bpmn-draw.js')
      await loadJS(baseUrl + 'modeler/display/bpmn-icons.js')
      await loadJS(baseUrl + 'modeler/display/Polyline.js')
      dispatch({
        type: 'SET_PREVIEW_DEPS_LOADED',
        data: true
      })
    } catch (error) {
      throw(error)
    }
  }
}

export const loadDesignDepsAction = () => {
  return async (dispatch) => {
    try {
      await loadCSS(baseUrl + 'modeler/editor-app/editor/css/editor.css')
      await loadJS(baseUrl + 'modeler/editor-app/libs/jquery.autogrow-textarea.js')
      await loadJS(baseUrl + 'modeler/editor-app/libs/prototype-1.7.3.js')
      await loadJS(baseUrl + 'modeler/editor-app/libs/mousetrap/1.6.0/mousetrap.min.js')
      await loadJS(baseUrl + 'modeler/editor-app/libs/path_parser.js')
      await loadJS(baseUrl + 'modeler/editor-app/editor/i18n/translation_en_us.js')
      await loadJS(baseUrl + 'modeler/editor-app/editor/i18n/translation_signavio_en_us.js')
      await loadJS(baseUrl + 'modeler/editor-app/editor/oryx.js')
      await loadJS(baseUrl + 'modeler/editor-app/eventbus.js')
      dispatch({
        type: 'SET_DESIGN_DEPS_LOADED',
        data: true
      })
    } catch (error) {
      throw (error)
    }
  }
}

export const loadJQueryAction = () => {
  return async (dispatch) => {
    try {
      await loadJS(baseUrl + 'modeler/libs/jquery_1.11.0/jquery.min.js')
      dispatch({
        type: 'SET_JQUERY_LOADED',
        data: true
      })
    } catch (error) {
      throw (error)
    }
  }
}

